<?php
	session_start();
	date_default_timezone_set('America/Los_Angeles');
include'connect_db.php';

	if(!isset($_SESSION['user']))
		die("Access denied");

	$user = $_SESSION['user'];

	$result = mysql_query("SELECT * FROM user WHERE id = $user");
	$row = mysql_fetch_array($result);
	$access = $row['type'];

	$order = mysql_real_escape_string($_POST['order']);

	$result = mysql_query("SELECT * FROM referral_chat WHERE referral_order = $order ORDER BY timestamp DESC");

	$returnArray = array();

	while($row = mysql_fetch_array($result))
	{
		$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
		$subrow = mysql_fetch_array($subresult);

		$row['name'] = $subrow['username'];
		$row['email'] = $subrow['paypal'];
		$row['type'] = $subrow['type'];
		$row['time'] = date("d M Y g:i:sA", $row['timestamp']);
		$row['avatar'] = md5( strtolower( trim($row['email']) ) );
		$row['message'] = stripslashes($row['message']);
		array_push($returnArray, $row);
	}

	echo json_encode($returnArray);
?>